package com.gongpingjialoan.view;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.adapter.SortAdapter;
import com.gongpingjialoan.bean.DealerCat;

import java.util.List;


public class SortPop {
    Context context;

    View contentV;

    PopupWindow pop;

    OnReslutClickListener onReslutClickListener;

    ListView listV;

    SortAdapter adapter;
    TextView text;

    public SortPop(Context context) {
        this.context = context;
        contentV = LayoutInflater.from(context)
                .inflate(R.layout.pop_sort, null);
        pop = new PopupWindow(contentV, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, true);
        // 需要设置一下此参数，点击外边可消失
        pop.setBackgroundDrawable(new BitmapDrawable());
        // 设置点击窗口外边窗口消失
        pop.setOutsideTouchable(true);
        // 设置此参数获得焦点，否则无法点击
        pop.setFocusable(true);
        // pop.setAnimationStyle(R.style.mystyle);
        pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        initView();

    }

    private void initView() {
        contentV.findViewById(R.id.bg).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        pop.dismiss();
                    }
                });

        listV = (ListView) contentV.findViewById(R.id.listview);
        adapter = new SortAdapter(context);
        listV.setAdapter(adapter);
        listV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setCurrentPosition(position);
                DealerCat data = adapter.getItem(position);
              //  text.setText(data.getName());
                if (onReslutClickListener != null) {
                    onReslutClickListener.result(data);
                }
                pop.dismiss();
            }
        });
    }

    public void setData(List<DealerCat> list) {
        adapter.setData(list);
    }

    public void setLabel(TextView text) {
        this.text = text;
    }

    public void show(View v) {
        pop.showAsDropDown(v);
    }

    public OnReslutClickListener getOnReslutClickListener() {
        return onReslutClickListener;
    }

    public void setOnReslutClickListener(
            OnReslutClickListener onReslutClickListener) {
        this.onReslutClickListener = onReslutClickListener;
    }

    public interface OnReslutClickListener {
        void result(DealerCat dealerCat);
    }

}
